from django.db import models
from django.urls import reverse
from django.utils import timezone
from slugify import slugify

from account.models import User
from static.help.getKeyWords import getSlugWord


class ArticleColumn(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='article_column')
    c_name = models.CharField(max_length=255)
    created = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.c_name


class Article(models.Model):
    u = models.ForeignKey(User, on_delete=models.CASCADE, related_name='articles')
    title = models.CharField(max_length=255)
    slug = models.SlugField(null=True,max_length=255)
    c_name = models.CharField(max_length=255)
    c = models.ForeignKey(ArticleColumn,on_delete=models.CASCADE)
    content = models.TextField(null=True)
    like = models.ManyToManyField(User,related_name="articles_like", blank=True)
    eye = models.ManyToManyField(User,related_name='articles_eye',blank=True)
    collect = models.ManyToManyField(User,related_name='articles_collect',blank=True)
    created = models.DateField(default=timezone.now)
    updated = models.DateField(auto_now=True)

    class Meta:
        ordering = ("title",)
        index_together = (('id', 'slug'),)

    def __str__(self):
        return self.title

    def save(self, *args, **kargs):
        self.slug = slugify(getSlugWord(self.title))
        super(Article, self).save(*args, **kargs)
